﻿Imports System.IO
Imports System.Threading
Imports System.Windows.Threading

Public Class 出售管理

#Region "加载型号"



    '加载型号
    Private Sub LoadXH(ByVal combox_a As ComboBox, ByVal combox_b As ComboBox, ByVal lb As Label)
        Try

            If combox_a.Text = "" Then
                lb.Text = "？"
                Exit Sub
            End If

            Dim sqlstr As String = "select 型号,备注 from 型号表 where 所属品牌 = '" + combox_a.Text + "'"
            Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
            combox_b.Items.Clear()
            Dim ds_table As Data.DataTable = ds.Tables(0)
            If ds_table.Rows.Count > 0 Then
                For i = 0 To ds_table.Rows.Count - 1
                    If (ds_table.Rows(i).Item("备注").ToString.Contains("隐藏")) And Not CheckBox2.Checked Then

                    Else
                        combox_b.Items.Add(ds_table.Rows(i).Item(0).ToString)
                    End If
                Next
            End If

        Catch ex As Exception

        End Try
    End Sub
    '行的使能
    Private Sub LineEN(ByVal en As Boolean, ByVal cb As ComboBox, ByVal lb As Label, ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal tx3 As TextBox, ByVal nu As NumericUpDown)
        If Not en Then
            cb.Items.Clear()
        End If

        cb.Enabled = en
        lb.Enabled = en
        tx1.Enabled = en
        tx2.Enabled = en
        tx3.Enabled = en
        nu.Enabled = en

        cb.SelectedIndex = -1
        lb.Text = "？"
        tx1.Text = ""
        tx1.Tag = ""
        tx2.Text = ""
        tx3.Text = ""
        nu.Value = 1

    End Sub
    '选择品牌
    Private Sub Cb1_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb1_1.SelectedIndexChanged
        If Cb1_1.Text = "不选择" Then
            LineEN(False, Cb1_2, LB1, tx1_1, tx1_2, tx1_3, NUD1)
        Else
            LineEN(True, Cb1_2, LB1, tx1_1, tx1_2, tx1_3, NUD1)
        End If
        LoadXH(Cb1_1, Cb1_2, LB1)
    End Sub
    Private Sub Cb2_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb2_1.SelectedIndexChanged
        If Cb2_1.Text = "不选择" Then
            LineEN(False, Cb2_2, LB2, tx2_1, tx2_2, tx2_3, NUD2)
        Else
            LineEN(True, Cb2_2, LB2, tx2_1, tx2_2, tx2_3, NUD2)
        End If
        LoadXH(Cb2_1, Cb2_2, LB2)
    End Sub
    Private Sub Cb3_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb3_1.SelectedIndexChanged
        If Cb3_1.Text = "不选择" Then
            LineEN(False, Cb3_2, LB3, tx3_1, tx3_2, tx3_3, NUD3)
        Else
            LineEN(True, Cb3_2, LB3, tx3_1, tx3_2, tx3_3, NUD3)
        End If
        LoadXH(Cb3_1, Cb3_2, LB3)
    End Sub
    Private Sub Cb4_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb4_1.SelectedIndexChanged
        If Cb4_1.Text = "不选择" Then
            LineEN(False, Cb4_2, LB4, tx4_1, tx4_2, tx4_3, NUD4)
        Else
            LineEN(True, Cb4_2, LB4, tx4_1, tx4_2, tx4_3, NUD4)
        End If
        LoadXH(Cb4_1, Cb4_2, LB4)
    End Sub
    Private Sub Cb5_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb5_1.SelectedIndexChanged
        If Cb5_1.Text = "不选择" Then
            LineEN(False, Cb5_2, LB5, tx5_1, tx5_2, tx5_3, NUD5)
        Else
            LineEN(True, Cb5_2, LB5, tx5_1, tx5_2, tx5_3, NUD5)
        End If
        LoadXH(Cb5_1, Cb5_2, LB5)
    End Sub
    Private Sub Cb6_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb6_1.SelectedIndexChanged
        If Cb6_1.Text = "不选择" Then
            LineEN(False, Cb6_2, LB6, tx6_1, tx6_2, tx6_3, NUD6)
        Else
            LineEN(True, Cb6_2, LB6, tx6_1, tx6_2, tx6_3, NUD6)
        End If
        LoadXH(Cb6_1, Cb6_2, LB6)
    End Sub
    Private Sub Cb7_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb7_1.SelectedIndexChanged
        If Cb7_1.Text = "不选择" Then
            LineEN(False, Cb7_2, LB7, tx7_1, tx7_2, tx7_3, NUD7)
        Else
            LineEN(True, Cb7_2, LB7, tx7_1, tx7_2, tx7_3, NUD7)
        End If
        LoadXH(Cb7_1, Cb7_2, LB7)
    End Sub
    '显示所有型号
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        ComboxChoosing = 0
        Cb1_1_SelectedIndexChanged(sender, e)
        Cb2_1_SelectedIndexChanged(sender, e)
        Cb3_1_SelectedIndexChanged(sender, e)
        Cb4_1_SelectedIndexChanged(sender, e)
        Cb5_1_SelectedIndexChanged(sender, e)
    End Sub

#End Region

#Region "选择型号"

    '选择型号
    Private Sub ChooseXH(ByVal cb1 As ComboBox, ByVal cb2 As ComboBox, ByVal lb As Label, ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal nu As NumericUpDown)

        If cb2.Text = "" Or cb1.Text = "" Then
            lb.Text = "？"
            Exit Sub
        End If

        Dim sqlstr As String = "select 型号,一般售价 from 型号表 where 所属品牌 = '" + cb1.Text + "' and 型号 ='" + cb2.Text + "'"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            tx1.Tag = ds_table.Rows(0).Item("一般售价").ToString
            tx1.Text = ""
            If CheckBox1.Checked Then tx1.Text = tx1.Tag.ToString
            tx2.Text = Val(tx1.Text) * nu.Value

            '读取库存
            Dim KC As Integer = sql_KC(cb1.Text, cb2.Text)
            lb.Text = KC.ToString

        End If

        '型号显长标志置零
        ComboxChoosing = 0

    End Sub
    Private Sub Cb1_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb1_2.SelectedIndexChanged
        ChooseXH(Cb1_1, Cb1_2, LB1, tx1_1, tx1_2, NUD1)
    End Sub
    Private Sub Cb2_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb2_2.SelectedIndexChanged
        ChooseXH(Cb2_1, Cb2_2, LB2, tx2_1, tx2_2, NUD2)
    End Sub
    Private Sub Cb3_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb3_2.SelectedIndexChanged
        ChooseXH(Cb3_1, Cb3_2, LB3, tx3_1, tx3_2, NUD3)
    End Sub
    Private Sub Cb4_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb4_2.SelectedIndexChanged
        ChooseXH(Cb4_1, Cb4_2, LB4, tx4_1, tx4_2, NUD4)
    End Sub
    Private Sub Cb5_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb5_2.SelectedIndexChanged
        ChooseXH(Cb5_1, Cb5_2, LB5, tx5_1, tx5_2, NUD5)
    End Sub
    Private Sub Cb6_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb6_2.SelectedIndexChanged
        ChooseXH(Cb6_1, Cb6_2, LB6, tx6_1, tx6_2, NUD6)
    End Sub
    Private Sub Cb7_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb7_2.SelectedIndexChanged
        ChooseXH(Cb7_1, Cb7_2, LB7, tx7_1, tx7_2, NUD7)
    End Sub

#End Region

#Region "输入检测"

    '只输入数字
    Private Sub NumOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub

    Private Sub tx1_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx1_1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx2_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx2_1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx3_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx3_1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx4_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx4_1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx5_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx5_1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx6_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx6_1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx7_1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx7_1.KeyPress
        NumOnly(e)
    End Sub

    Private Sub tx1_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx1_2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx2_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx2_2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx3_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx3_2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx4_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx4_2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx5_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx5_2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx6_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx6_2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub tx7_2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tx7_2.KeyPress
        NumOnly(e)
    End Sub

#End Region

#Region "出售操作"

    '出售键
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        aa = 0
        Dim bb As Integer = 0

        If Cb1_1.SelectedIndex = 0 Then
            MsgBox("最少出售一件商品才能执行出售操作")
            Exit Sub
        End If

        TypeCheck(Cb1_1, Cb1_2, LB1, tx1_1, tx1_2, tx1_3, NUD1) : bb += 1
        If aa <> bb Then Exit Sub

        If Cb2_1.SelectedIndex > 0 Then
            TypeCheck(Cb2_1, Cb2_2, LB2, tx2_1, tx2_2, tx2_3, NUD2) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb3_1.SelectedIndex > 0 Then
            TypeCheck(Cb3_1, Cb3_2, LB3, tx3_1, tx3_2, tx3_3, NUD3) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb4_1.SelectedIndex > 0 Then
            TypeCheck(Cb4_1, Cb4_2, LB4, tx4_1, tx4_2, tx4_3, NUD4) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb5_1.SelectedIndex > 0 Then
            TypeCheck(Cb5_1, Cb5_2, LB5, tx5_1, tx5_2, tx5_3, NUD5) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb6_1.SelectedIndex > 0 Then
            TypeCheck(Cb6_1, Cb6_2, LB6, tx6_1, tx6_2, tx6_3, NUD6) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb7_1.SelectedIndex > 0 Then
            TypeCheck(Cb7_1, Cb7_2, LB7, tx7_1, tx7_2, tx7_3, NUD7) : bb += 1
            If aa <> bb Then Exit Sub
        End If


        '写入出售表
        writeRS(Cb1_1, Cb1_2, LB1, tx1_1, tx1_2, tx1_3, NUD1)

        If Cb2_1.SelectedIndex > 0 Then writeRS(Cb2_1, Cb2_2, LB2, tx2_1, tx2_2, tx2_3, NUD2)

        If Cb3_1.SelectedIndex > 0 Then writeRS(Cb3_1, Cb3_2, LB3, tx3_1, tx3_2, tx3_3, NUD3)

        If Cb4_1.SelectedIndex > 0 Then writeRS(Cb4_1, Cb4_2, LB4, tx4_1, tx4_2, tx4_3, NUD4)

        If Cb5_1.SelectedIndex > 0 Then writeRS(Cb5_1, Cb5_2, LB5, tx5_1, tx5_2, tx5_3, NUD5)

        If Cb6_1.SelectedIndex > 0 Then writeRS(Cb6_1, Cb6_2, LB6, tx6_1, tx6_2, tx6_3, NUD6)

        If Cb7_1.SelectedIndex > 0 Then writeRS(Cb7_1, Cb7_2, LB7, tx7_1, tx7_2, tx7_3, NUD7)

        '写入单号表
        Dim sqlstr As String = ""
        sqlstr += "insert into 单号表(单号,单号头,序号) values("
        sqlstr += "'" + DH + "',"
        sqlstr += "'" + DHH + "',"
        sqlstr += "'" + SHW + "'"
        sqlstr += ")"
        SQLupdata(sqlstr)

        MsgBox("出售操作成功")

        TextBox31.Text = ""
        TextBox32.Text = ""
        TextBox33.Text = ""
        LB_aa.Text = ""
        DH_calc()
        TextBox31.Focus()

    End Sub
    '输入检测
    Private Sub TypeCheck(ByVal cb1 As ComboBox, ByVal cb2 As ComboBox, ByVal lb As Label, ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal tx3 As TextBox, ByVal nu As NumericUpDown)
        '变量赋值
        Dim Kname As String = TextBox31.Text
        Dim Kphon As String = TextBox32.Text
        Dim Kplac As String = TextBox33.Text

        Dim PP As String = cb1.Text
        Dim XH As String = cb2.Text
        Dim JG As String = tx1.Text
        Dim ZE As String = tx2.Text
        Dim SL As String = nu.Value
        Dim RQ As String = Now.ToString
        Dim BZ As String = tx3.Text

        '格式条件检测
        Dim s1 As Boolean = (Trim(Kname) = "")
        Dim s2 As Boolean = (Trim(Kphon) = "")
        Dim s3 As Boolean = (Trim(Kplac) = "")
        Dim s4 As Boolean = (PP = "")
        Dim s5 As Boolean = (XH = "")
        Dim s6 As Boolean = ((Not IsNumeric(JG)) Or Trim(JG) = "")
        Dim s7 As Boolean = ((Not IsNumeric(ZE)) Or Trim(ZE) = "")

        If s1 Then MsgBox("必要的信息未完成输入，请键入客户称乎！") : TextBox31.Focus() : Exit Sub
        If s2 Then MsgBox("必要的信息未完成输入，请键入客户联系方式！") : TextBox32.Focus() : Exit Sub
        If s3 Then MsgBox("必要的信息未完成输入，请键入客户地址！") : TextBox33.Focus() : Exit Sub

        If s4 Then MsgBox("请选择出售商品的品牌") : cb1.Focus() : Exit Sub
        If s5 Then MsgBox("请选择出售商品的型号") : cb2.Focus() : Exit Sub
        If s6 Then MsgBox("请入正确的数字！") : tx1.Focus() : Exit Sub
        If s7 Then MsgBox("请入正确的数字！") : tx2.Focus() : Exit Sub
        If Val(lb.Text) < nu.Value Then MsgBox("‘" + PP + " " + XH + "’ 的库存量不足" + nu.Value.ToString + "台") : cb1.Focus() : Exit Sub

        aa += 1

    End Sub
    '写入数据库
    Private Sub writeRS(ByVal cb1 As ComboBox, ByVal cb2 As ComboBox, ByVal lb As Label, ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal tx3 As TextBox, ByVal nu As NumericUpDown)

        Dim Kname As String = TextBox31.Text
        Dim Kphon As String = TextBox32.Text
        Dim Kplac As String = TextBox33.Text

        Dim PP As String = cb1.Text
        Dim XH As String = cb2.Text
        Dim JG As String = tx1.Text
        Dim ZE As String = tx2.Text
        Dim SL As String = nu.Value
        Dim RQ As String = Now.ToString
        Dim BZ As String = tx3.Text

        '写入数据库
        Dim sqlstr As String = ""
        sqlstr += "insert into 出售表(品牌,型号,售出数量,购机客户,地址,联系电话,售出单价,出售总额,日期,备注1,单号) values("
        sqlstr += "'" + PP + "',"
        sqlstr += "'" + XH + "',"
        sqlstr += "'" + SL + "',"
        sqlstr += "'" + Kname + "',"
        sqlstr += "'" + Kplac + "',"
        sqlstr += "'" + Kphon + "',"
        sqlstr += "'" + JG + "',"
        sqlstr += "'" + ZE + "',"
        sqlstr += "'" + RQ + "',"
        sqlstr += "'" + BZ + "',"
        sqlstr += "'" + DH + "'"
        sqlstr += ")"
        SQLupdata(sqlstr)


        '控件内容处理
        cb1.SelectedIndex = 0
        cb2.Items.Clear()
        cb2.SelectedIndex = -1
        tx1.Text = ""
        tx2.Text = ""
        tx3.Text = ""
        lb.Text = "？"
        nu.Value = 1

    End Sub

#End Region

#Region "改变数量"

    '改变数量
    Private Sub Nuchange(ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal nu As NumericUpDown)
        If tx1.Text <> "" Then
            tx2.Text = (Val(tx1.Text) * nu.Value).ToString
        End If
    End Sub

    Private Sub tx1_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx1_1.TextChanged
        Nuchange(tx1_1, tx1_2, NUD1)
    End Sub
    Private Sub tx2_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx2_1.TextChanged
        Nuchange(tx2_1, tx2_2, NUD2)
    End Sub
    Private Sub tx3_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx3_1.TextChanged
        Nuchange(tx3_1, tx3_2, NUD3)
    End Sub
    Private Sub tx4_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx4_1.TextChanged
        Nuchange(tx4_1, tx4_2, NUD4)
    End Sub
    Private Sub tx5_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx5_1.TextChanged
        Nuchange(tx5_1, tx5_2, NUD5)
    End Sub
    Private Sub tx6_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx6_1.TextChanged
        Nuchange(tx6_1, tx6_2, NUD6)
    End Sub
    Private Sub tx7_1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tx7_1.TextChanged
        Nuchange(tx7_1, tx7_2, NUD7)
    End Sub

    Private Sub NUD1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD1.ValueChanged
        Nuchange(tx1_1, tx1_2, NUD1)
    End Sub
    Private Sub NUD2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD2.ValueChanged
        Nuchange(tx2_1, tx2_2, NUD2)
    End Sub
    Private Sub NUD3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD3.ValueChanged
        Nuchange(tx3_1, tx3_2, NUD3)
    End Sub
    Private Sub NUD4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD4.ValueChanged
        Nuchange(tx4_1, tx4_2, NUD4)
    End Sub
    Private Sub NUD5_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD5.ValueChanged
        Nuchange(tx5_1, tx5_2, NUD5)
    End Sub
    Private Sub NUD6_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD6.ValueChanged
        Nuchange(tx6_1, tx6_2, NUD6)
    End Sub
    Private Sub NUD7_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUD7.ValueChanged
        Nuchange(tx7_1, tx7_2, NUD7)
    End Sub

#End Region

    Dim DHH As String       '单号头
    Dim SHW As String       '单号尾
    Dim DH As String        '单号

    Dim aa As Integer       '空值检测变量

    Dim ZT_title As Font = New Font(New FontFamily("黑体"), 18)         '标题字体
    Dim ZT_Littl As Font = New Font(New FontFamily("宋体"), 12)         '页眉字体

    Dim ZT_phone As Font = New Font(New FontFamily("幼圆"), 10)         '页脚字体1
    Dim ZT_litt2 As Font = New Font(New FontFamily("幼圆"), 8)          '页脚字体2
    Dim ZT_titl2 As Font = New Font(New FontFamily("幼圆"), 15)         '标题字体2

    Dim ComboxChoosing As Integer = 0
    Dim th As Thread

    '关闭窗体
    Private Sub 出售管理_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        th.Abort()
        Commodsell = Nothing
    End Sub

    '窗体加载
    Private Sub 出售管理_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Control.CheckForIllegalCrossThreadCalls = False

        '显示即将选择的机型（后台线程）
        th = New Thread(
            New ThreadStart(
            Sub()
                Try
                    Label4.Text = ""
                    Label13.Visible = False
                    While True
                        Select Case ComboxChoosing
                            Case 1
                                If Cb1_2.SelectedIndex >= 0 And Cb1_2.SelectedItem IsNot Nothing Then
                                    Label4.Text = Cb1_2.SelectedItem.ToString()
                                    Label13.Visible = True
                                End If
                            Case 2
                                If Cb2_2.SelectedIndex >= 0 And Cb2_2.SelectedItem IsNot Nothing Then
                                    Label4.Text = Cb2_2.SelectedItem.ToString()
                                    Label13.Visible = True
                                End If
                            Case 3
                                If Cb3_2.SelectedIndex >= 0 And Cb3_2.SelectedItem IsNot Nothing Then
                                    Label4.Text = Cb3_2.SelectedItem.ToString()
                                    Label13.Visible = True
                                End If
                            Case 4
                                If Cb4_2.SelectedIndex >= 0 And Cb4_2.SelectedItem IsNot Nothing Then
                                    Label4.Text = Cb4_2.SelectedItem.ToString()
                                    Label13.Visible = True
                                End If
                            Case 5
                                If Cb5_2.SelectedIndex >= 0 And Cb5_2.SelectedItem IsNot Nothing Then
                                    Label4.Text = Cb5_2.SelectedItem.ToString()
                                    Label13.Visible = True
                                End If
                            Case Else
                                Label4.Text = ""
                                Label13.Visible = False
                        End Select
                    End While
                Catch

                End Try

            End Sub))

        '线程启动
        th.IsBackground = True
        th.Start()

        '加载品牌
        Dim sqlstr As String = "select 品牌 from 品牌表"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            Cb1_1.Items.Add("不选择")
            Cb2_1.Items.Add("不选择")
            Cb3_1.Items.Add("不选择")
            Cb4_1.Items.Add("不选择")
            Cb5_1.Items.Add("不选择")
            Cb6_1.Items.Add("不选择")
            Cb7_1.Items.Add("不选择")
            For i = 0 To ds_table.Rows.Count - 1
                Cb1_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Cb2_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Cb3_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Cb4_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Cb5_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Cb6_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Cb7_1.Items.Add(ds_table.Rows(i).Item(0).ToString)
            Next
        End If

    End Sub
    Private Sub 出售管理_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Cb1_1.SelectedIndex = 0
        Cb2_1.SelectedIndex = 0
        Cb3_1.SelectedIndex = 0
        Cb4_1.SelectedIndex = 0
        Cb5_1.SelectedIndex = 0
        Cb6_1.SelectedIndex = 0
        Cb7_1.SelectedIndex = 0

        Cb6_1.Enabled = False
        Cb7_1.Enabled = False

        LB_aa.Text = ""
        DH_calc()
        TextBox31.Focus()
    End Sub
    '生成单号
    Private Sub DH_calc()

        Dim Flag1 As Boolean   '单号表是否存在
        Dim Flag2 As Boolean   '单号字段是否存在

        '判断单号表是否存在
        Try
            Dim SqlSearch As String = "select * from 单号表"
            Dim ds11 As DataSet = SQLtoDataSet(SqlSearch)
            If ds11.Tables.Count > 0 Then
                Flag1 = True     '存在
            End If
        Catch ex As Exception
            Flag1 = False        '不存在
        End Try

        '建表完成
        If Not Flag1 Then
            Dim SqlTableCA As String = "create table 单号表 ( ID integer IDENTITY (1,1) PRIMARY KEY ,单号 char(13) not null,单号头 char(13) not null,序号 integer not null)"
            SQLupdata(SqlTableCA)
        End If

        '判断单号字段是否存在
        Try
            Dim SqlSearch As String = "select 单号 from 出售表 "
            Dim ds12 As DataSet = SQLtoDataSet(SqlSearch)
            If ds12.Tables.Count > 0 Then
                Flag2 = True     '存在
            End If
        Catch ex As Exception
            Flag2 = False        '不存在

        End Try

        '增加列完成
        If Not Flag2 Then
            Dim SqlTableCA As String = "alter table 出售表 add 单号 char(13)"
            SQLupdata(SqlTableCA)
        End If

        '开始生成单号

        '单号头部
        Dim mon As String = Now.Month.ToString
        If Now.Month < 10 Then
            mon = "0" + Now.Month.ToString
        End If

        Dim daa As String = Now.Day.ToString
        If Now.Day < 10 Then
            daa = "0" + Now.Day.ToString
        End If

        DHH = Now.Year.ToString + mon + daa

        '计算单号
        Dim sql As String = "select 序号 from 单号表  where 单号头 ='" + DHH + "'"
        Dim ds As DataSet = SQLtoDataSet(sql)
        If ds.Tables(0).Rows.Count > 0 Then
            Dim sh As Integer = ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1).Item(0)
            If sh > 99 Then
                SHW = (sh + 1).ToString
            ElseIf sh > 9 Then
                SHW = "0" + (sh + 1).ToString
            Else
                SHW = "00" + (sh + 1).ToString
            End If
        Else
            SHW = "001"
        End If

        '完成单号生成
        DH = DHH + SHW
        LB_aa.Text = DH


    End Sub

    '打印
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        aa = 0
        Dim bb As Integer = 0

        If Cb1_1.SelectedIndex = 0 Then
            MsgBox("最少出售一件商品,并且登记客户必要的信息，才能执行打印操作")
            Exit Sub
        End If


        TypeCheck(Cb1_1, Cb1_2, LB1, tx1_1, tx1_2, tx1_3, NUD1) : bb += 1
        If aa <> bb Then Exit Sub

        If Cb2_1.SelectedIndex > 0 Then
            TypeCheck(Cb2_1, Cb2_2, LB2, tx2_1, tx2_2, tx2_3, NUD2) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb3_1.SelectedIndex > 0 Then
            TypeCheck(Cb3_1, Cb3_2, LB3, tx3_1, tx3_2, tx3_3, NUD3) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb4_1.SelectedIndex > 0 Then
            TypeCheck(Cb4_1, Cb4_2, LB4, tx4_1, tx4_2, tx4_3, NUD4) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb5_1.SelectedIndex > 0 Then
            TypeCheck(Cb5_1, Cb5_2, LB5, tx5_1, tx5_2, tx5_3, NUD5) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb6_1.SelectedIndex > 0 Then
            TypeCheck(Cb6_1, Cb6_2, LB6, tx6_1, tx6_2, tx6_3, NUD6) : bb += 1
            If aa <> bb Then Exit Sub
        End If
        If Cb7_1.SelectedIndex > 0 Then
            TypeCheck(Cb7_1, Cb7_2, LB7, tx7_1, tx7_2, tx7_3, NUD7) : bb += 1
            If aa <> bb Then Exit Sub
        End If

        '强制横向打印

        '预览

        CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
        PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize(Printing.PaperKind.Custom, 815, 510)

        PrintPreviewDialog1.ShowDialog()


    End Sub
    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        '页眉
        g.DrawLine(Pens.Black, 40, 40, 810 - 50, 40)
        g.DrawString("买得放心  用得开心", ZT_litt2, Brushes.Black, 40, 23)
        g.DrawString("绍康电器  信心的保证", ZT_litt2, Brushes.Black, 693 - 50, 23)

        '标题：原设置常化设置，现改出这里的读写设置
        Dim TiS As TitleSet = ReadTx("SEL.txt")
        g.DrawString(TiS.T1, ZT_title, Brushes.Black, TiS.T1x, TiS.T1y)
        g.DrawString(TiS.T2, ZT_title, Brushes.Black, TiS.T2x, TiS.T2y)
        g.DrawString(TiS.T3, ZT_title, Brushes.Black, TiS.T3x, TiS.T3y)

        '标题：原设置常化设置，现改出上面的读写设置
        'g.DrawString("东 莞 市 虎 门 伟 康 冷 冻 设 备", ZT_title, Brushes.Black, 213 - 50, 50)
        'g.DrawString("东 莞 市 虎 门 绍 康 电 器 商 店", ZT_title, Brushes.Black, 213 - 50, 95)
        'g.DrawString("商 品 销 售 单", ZT_title, Brushes.Black, 325 - 50 + 3, 135)

        '电话、电址
        g.DrawString("电话：13450000055 13902613906 85512003", ZT_phone, Brushes.Black, 220, 510 - 50)
        g.DrawString("地址：虎门执信路43号", ZT_phone, Brushes.Black, 40, 510 - 50)

        '打印日期、出单日期
        'g.DrawString("打印时间：" + Now.ToString, ZT_litt2, Brushes.Black, 650 - 50, 515 - 50)
        g.DrawString("日期：" + Now.ToString, ZT_phone, Brushes.Black, 640 - 50, 185)

        '客户、地址
        g.DrawString(TextBox31.Text, ZT_titl2, Brushes.Black, 40, 155)
        g.DrawString("联系：" + TextBox32.Text, ZT_phone, Brushes.Black, 42, 185)
        g.DrawString(TextBox33.Text, ZT_phone, Brushes.Black, 220, 185)

        '单号：
        g.DrawString("N0." + LB_aa.Text, ZT_titl2, Brushes.Black, 640 - 50, 157)

        '表格
        DrawTable(g, pen, rect)

        '表头
        g.DrawString("品 名 规 格", ZT_phone, Brushes.Black, 100 - 25, 220)
        g.DrawString("单 价", ZT_phone, Brushes.Black, 340 - 50, 220)
        g.DrawString("数 量", ZT_phone, Brushes.Black, 467 - 50, 220)
        g.DrawString("小 计", ZT_phone, Brushes.Black, 594 - 50, 220)
        g.DrawString("备 注", ZT_phone, Brushes.Black, 721 - 50, 220)

        '填表
        DrawList(g, pen, rect)
    End Sub
    '画表
    Private Sub DrawTable(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        '竖边
        g.DrawLine(Pens.Black, 41, 217, 41, 496 - 60)
        g.DrawLine(Pens.Black, 809 - 50, 217, 809 - 50, 496 - 60)

        '横边
        g.DrawLine(Pens.Black, 40, 213, 810 - 50, 213)
        g.DrawLine(Pens.Black, 48, 241, 802 - 50, 241)
        g.DrawLine(Pens.Black, 40, 500 - 60, 810 - 50, 500 - 60)
        g.DrawLine(Pens.Black, 48, 451 - 60, 802 - 50, 451 - 60)

        '内横
        For i = 1 To 6 - 2
            g.DrawLine(Pens.Black, 48, 241 + (30 * i), 802 - 50, 241 + (30 * i))
        Next

        '内竖--中线
        g.DrawLine(Pens.Black, 425 - 50, 217, 425 - 50, 237)
        g.DrawLine(Pens.Black, 425 - 50, 245, 425 - 50, 447 - 60)

        '内竖--后三分
        g.DrawLine(Pens.Black, 553 - 50, 217, 553 - 50, 237)
        g.DrawLine(Pens.Black, 553 - 50, 245, 553 - 50, 447 - 60)
        g.DrawLine(Pens.Black, 553 - 50, 455 - 60, 553 - 50, 496 - 60)

        g.DrawLine(Pens.Black, 681 - 50, 217, 681 - 50, 237)
        g.DrawLine(Pens.Black, 681 - 50, 245, 681 - 50, 447 - 60)
        g.DrawLine(Pens.Black, 681 - 50, 455 - 60, 681 - 50, 496 - 60)


        '内竖--前三份显一
        g.DrawLine(Pens.Black, 296 - 50, 217, 296 - 50, 237)
        g.DrawLine(Pens.Black, 296 - 50, 245, 296 - 50, 447 - 60)

        g.DrawLine(Pens.Black, 168 - 50, 455 - 60, 168 - 50, 496 - 60)

    End Sub
    '填表
    Private Sub DrawList(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        Dim ZE As Integer = 0
        Dim i As Integer = 0

        If Cb1_1.Text <> "不选择" Then
            writeTable(g, Cb1_1.Text, Cb1_2.Text, tx1_1.Text, NUD1.Value, tx1_2.Text, tx1_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx1_2.Text)
            i += 1
        End If
        If Cb2_1.Text <> "不选择" Then
            writeTable(g, Cb2_1.Text, Cb2_2.Text, tx2_1.Text, NUD2.Value, tx2_2.Text, tx2_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx2_2.Text)
            i += 1
        End If
        If Cb3_1.Text <> "不选择" Then
            writeTable(g, Cb3_1.Text, Cb3_2.Text, tx3_1.Text, NUD3.Value, tx3_2.Text, tx3_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx3_2.Text)
            i += 1
        End If
        If Cb4_1.Text <> "不选择" Then
            writeTable(g, Cb4_1.Text, Cb4_2.Text, tx4_1.Text, NUD4.Value, tx4_2.Text, tx4_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx4_2.Text)
            i += 1
        End If
        If Cb5_1.Text <> "不选择" Then
            writeTable(g, Cb5_1.Text, Cb5_2.Text, tx5_1.Text, NUD5.Value, tx5_2.Text, tx5_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx5_2.Text)
            i += 1
        End If
        If Cb6_1.Text <> "不选择" Then
            writeTable(g, Cb6_1.Text, Cb6_2.Text, tx6_1.Text, NUD6.Value, tx6_2.Text, tx6_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx6_2.Text)
            i += 1
        End If
        If Cb7_1.Text <> "不选择" Then
            writeTable(g, Cb7_1.Text, Cb7_2.Text, tx7_1.Text, NUD7.Value, tx7_2.Text, tx7_3.Text, 220 + (i + 1) * 30)
            ZE += Val(tx7_2.Text)
            i += 1
        End If


        Dim bcount As Integer = 7 - 2 - i
        While bcount > 0
            g.DrawString("/", ZT_phone, Brushes.Black, 100 - 25, 220 + (i + 1) * 30)
            g.DrawString("/", ZT_phone, Brushes.Black, 340 - 50, 220 + (i + 1) * 30)
            g.DrawString("/", ZT_phone, Brushes.Black, 467 - 50, 220 + (i + 1) * 30)
            g.DrawString("/", ZT_phone, Brushes.Black, 594 - 50, 220 + (i + 1) * 30)
            i += 1
            bcount -= 1
        End While

        '总计
        g.DrawString("￥" + ZE.ToString, ZT_titl2, Brushes.Black, 580 - 50, 467 - 60)

        g.DrawString("合 计", ZT_phone, Brushes.Black, 55, 470 - 60)
        g.DrawString("大写：" + RMBtoBig(ZE), ZT_phone, Brushes.Black, 180 - 50, 470 - 60)


        g.DrawString("经办人：" + AccountName, ZT_phone, Brushes.Black, 690 - 50, 470 - 60)

    End Sub
    Private Sub writeTable(ByVal g As Graphics, ByVal PP As String, ByVal XH As String, ByVal DJ As String, ByVal SL As String, ByVal XJ As String, ByVal BZ As String, ByVal y As Integer)
        g.DrawString(PP + "  " + XH, ZT_phone, Brushes.Black, 100 - 25, y)
        g.DrawString("￥" + DJ, ZT_phone, Brushes.Black, 340 - 50, y)
        g.DrawString(SL + "台", ZT_phone, Brushes.Black, 467 - 50, y)
        g.DrawString("￥" + XJ, ZT_phone, Brushes.Black, 594 - 50, y)
        g.DrawString(BZ, ZT_litt2, Brushes.Black, 640, y)

    End Sub

    '显示单价
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            tx1_1.Text = tx1_1.Tag.ToString
            tx2_1.Text = tx2_1.Tag.ToString
            tx3_1.Text = tx3_1.Tag.ToString
            tx4_1.Text = tx4_1.Tag.ToString
            tx5_1.Text = tx5_1.Tag.ToString
        Else
            tx1_1.Text = ""
            tx2_1.Text = ""
            tx3_1.Text = ""
            tx4_1.Text = ""
            tx5_1.Text = ""
        End If
    End Sub


#Region "型号显长标志赋值"
    Private Sub Cb1_2_Click(sender As Object, e As EventArgs) Handles Cb1_2.Click
        ComboxChoosing = 1
    End Sub
    Private Sub Cb2_2_Click(sender As Object, e As EventArgs) Handles Cb2_2.Click
        ComboxChoosing = 2
    End Sub
    Private Sub Cb3_2_Click(sender As Object, e As EventArgs) Handles Cb3_2.Click
        ComboxChoosing = 3
    End Sub
    Private Sub Cb4_2_Click(sender As Object, e As EventArgs) Handles Cb4_2.Click
        ComboxChoosing = 4
    End Sub
    Private Sub Cb5_2_Click(sender As Object, e As EventArgs) Handles Cb5_2.Click
        ComboxChoosing = 5
    End Sub


    Private Sub Cb1_2_DropDownClosed(sender As Object, e As EventArgs) Handles Cb1_2.DropDownClosed
        ComboxChoosing = 0
    End Sub
    Private Sub Cb2_2_DropDownClosed(sender As Object, e As EventArgs) Handles Cb2_2.DropDownClosed
        ComboxChoosing = 0
    End Sub
    Private Sub Cb3_2_DropDownClosed(sender As Object, e As EventArgs) Handles Cb3_2.DropDownClosed
        ComboxChoosing = 0
    End Sub
    Private Sub Cb4_2_DropDownClosed(sender As Object, e As EventArgs) Handles Cb4_2.DropDownClosed
        ComboxChoosing = 0
    End Sub
    Private Sub Cb5_2_DropDownClosed(sender As Object, e As EventArgs) Handles Cb5_2.DropDownClosed
        ComboxChoosing = 0
    End Sub


#End Region


End Class